package com.example.test.service.impl;

import com.example.test.entity.User;
import com.example.test.mapper.UserMapper;
import com.example.test.service.UserService;
import com.example.test.util.MyBatisUtil;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public Object findAll() throws IOException {
        MyBatisUtil myBatisUtil = new MyBatisUtil();
        UserMapper userMapper = myBatisUtil.userMapperLoad();
        List<User> all = userMapper.findAll();
        myBatisUtil.closeSqlSession();
        return all;
    }

    @Override
    public Object insert() throws IOException {
        MyBatisUtil myBatisUtil = new MyBatisUtil();
        User user = new User();
        user.setUsername("abd");
        user.setPassword("abd");
        user.setRole_id(200);
        UserMapper insert = myBatisUtil.userMapperLoad("insert", user, true);
        List<User> all = insert.findAll();
        myBatisUtil.closeSqlSession();
        return all;
    }

    @Override
    public Object delete() throws IOException {
        MyBatisUtil myBatisUtil = new MyBatisUtil();
        UserMapper delete = myBatisUtil.userMapperLoad("delete", 3, true);
        List<User> all = delete.findAll();
        myBatisUtil.closeSqlSession();
        return all;
    }

    @Override
    public Object update() throws IOException {
        MyBatisUtil myBatisUtil = new MyBatisUtil();
        User user = new User();
        user.setId(3);
        user.setUsername("abd");
        UserMapper update = myBatisUtil.userMapperLoad("update", user, true);
        List<User> all = update.findAll();
        myBatisUtil.closeSqlSession();
        return all;
    }

    @Override
    public Object getWithRole() throws IOException {
        MyBatisUtil myBatisUtil = new MyBatisUtil();
        UserMapper userMapper = myBatisUtil.userMapperLoad();
        List<User> allWithRole = userMapper.findAllWithRole();
        return allWithRole;
    }

    @Override
    public Object findRepos() throws IOException {
        MyBatisUtil myBatisUtil = new MyBatisUtil();
        UserMapper userMapper = myBatisUtil.userMapperLoad();
        List<User> repos = userMapper.findRepos();
        return  repos;
    }
}
